package club.emergency.wechat.resource_study.operate.provider.impl;

import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.fastdfs.FastDfsService;
import club.emergency.wechat.model.resource_study.ResourceStudy;
import club.emergency.wechat.persistence.resource_study.ResourceStudyMapper;
import club.emergency.wechat.resource_study.operate.ResourceStudyOperate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create with IntelliJ IDEA
 * Project Name: business
 * Package Name: club.emergency.wechat.resource_study.operate.provider.impl
 * Date: 2018/12/21
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
@Transactional(rollbackFor = Exception.class)
public class ResourceStudyOperateImpl implements ResourceStudyOperate {

    private ResourceStudyMapper resourceStudyMapper;

    private FastDfsService fastDfsMapper;

    public ResourceStudyOperateImpl(
            ResourceStudyMapper resourceStudyMapper,
            FastDfsService fastDfsMapper
    ) {
        this.resourceStudyMapper = resourceStudyMapper;
        this.fastDfsMapper = fastDfsMapper;
    }

    @Override
    public String uploadFile(@RequestBody byte[] bytes, @RequestParam("extName") String extName, @RequestParam("fileName") String fileName) {
        return fastDfsMapper.upload(bytes, extName, fileName);
    }

    @Override
    public String deleteFile(@RequestParam("id") Long id) {
        return fastDfsMapper.deleteById(id);
    }

    @Override
    public String save(@RequestBody ResourceStudy resourceStudy) {
        resourceStudyMapper.insert(resourceStudy);
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "资料学习记录新增成功！", resourceStudy.getId());
    }

    @Override
    public String modify(@RequestBody ResourceStudy resourceStudy) {
        resourceStudyMapper.updateById(resourceStudy);
        return ResponseEntity.ok(StatusCode.DB_MODIFY_OK, "资料学习记录更新成功！");
    }

    @Override
    public String remove(@RequestBody ResourceStudy resourceStudy) {
        deleteFile(resourceStudy.getResourceFileId());
        resourceStudyMapper.deleteById(resourceStudy.getId());
        return ResponseEntity.ok(StatusCode.DB_MODIFY_OK, "资料学习记录删除成功！");
    }

}
